<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Forums" %>

<%@ Register TagPrefix="Club" TagName="WebContent" Src="~/UserControls/WebContent.ascx" %>


<script runat="server">
#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
    Private Function GetIcon(ByVal section As Boolean, ByVal locked As Boolean) As String
        If section Then
            
            Return "~/app_themes/" & Page.Theme.ToString & "/images/section.gif"
        Else
            If locked Then
                Return "~/app_themes/" & Page.Theme.ToString & "/images/lockedtopic.gif"
            Else
                Return "~/app_themes/" & Page.Theme.ToString & "/images/topic.gif"
            End If
        End If
    End Function

    Private Function GetIconText(ByVal section As Boolean, ByVal locked As Boolean) As String
        If section Then
            Return "Section"
        Else
            If locked Then
                Return "Locked Topic"
            Else
                Return "Topic"
            End If
        End If
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Panel1.Visible = User.IsInRole("Administrators")
            Panel2.Visible = User.Identity.IsAuthenticated
            SqlDataSource2.CancelSelectOnNullParameter = True
            If User.Identity.IsAuthenticated Then
                SqlDataSource2.SelectParameters("memberid").DefaultValue = Membership.GetUser.ProviderUserKey.ToString()
            End If
        End If
    End Sub
    
    Protected Function IsRecentPost(ByVal lastpostdate As Object) As Boolean
        Return ClubStarterKit.Web.Forum.IsRecentPost(lastpostdate)
    End Function
    
    Protected Function RecentPostAge(ByVal lastpostdate As Object) As String
        Return ClubStarterKit.Web.Forum.RecentPostAge(lastpostdate)
    End Function
    
    Protected Function IsUnreadTopic(ByVal topicid As Object) As Boolean
        If Not User.Identity.IsAuthenticated Then Return False
        Return ClubStarterKit.Web.Forum.TopicHasUnreadMessages(CInt(topicid), New Guid(Membership.GetUser.ProviderUserKey.ToString()))
    End Function
    
    Protected Function IsUnreadThread(ByVal lastpostdate As Object, ByVal threadid As Object) As Boolean
        If Not User.Identity.IsAuthenticated Then Return False
        If lastpostdate Is Nothing Or IsDBNull(lastpostdate) Then Return False
        Return ClubStarterKit.Web.Forum.IsThreadUnread(CInt(threadid), New Guid(Membership.GetUser.ProviderUserKey.ToString()), CDate(lastpostdate))
    End Function
    
    Protected Sub Unwatch_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        If Not User.Identity.IsAuthenticated Then Return
        ClubStarterKit.Web.Forum.SetThreadWatch(CInt(e.CommandArgument()), New Guid(Membership.GetUser.ProviderUserKey.ToString()), False)
        Repeater2.DataBind()
    End Sub

    Protected Sub MarkReadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not User.Identity.IsAuthenticated Then Return
        ClubStarterKit.Web.Forum.MarkAllRead(New Guid(Membership.GetUser.ProviderUserKey.ToString()))
        Repeater1.DataBind()
        Repeater2.DataBind()
    End Sub

    Protected Sub ForumProfileButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not User.Identity.IsAuthenticated Then Return
        Response.Redirect("profile.aspx?id=" & Membership.GetUser.ProviderUserKey.ToString())
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <div id="columnleft">
            <a name="content_start" id="content_start"></a>
            <div class="leftblock">
            <Club:WebContent ID="WebContent1" runat="server" Section="forumhome" />
                    <a href="../RSS/AllPostsRss.aspx">
                    <asp:Image ID="Image4" runat="server" SkinID="rss" />
                    </a>
                <asp:Panel ID="Panel1" runat="server">
                    <Club:RolloverLink ID="HyperLink3" Style="font-size:14px;" NavigateUrl="Manage.aspx" runat="server" Text="Mannage Forum" />
                </asp:Panel>
                <br />
                &nbsp;</div>
        </div>
        <div id="columnright">
            <div class="rightblock">
                &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                    SelectCommand="GetTopics" SelectCommandType="StoredProcedure" DataSourceMode="DataReader">
                </asp:SqlDataSource>
                <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                    <HeaderTemplate>
                    <h3>Forum Threads</h3>
                        <br />
                        <table cellspacing="0">
                            <tr>
                                <td colspan="2">
                                    
                                </td>
                                <td align="right" colspan="2">
                                    
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr id="Tr1" runat="server" class='<%# iif( CType(Eval("section"), boolean), "sectionrow", "forumrow") %>'>
                            <td width="24" align="center" class="forumleftcell">
                                <asp:Image ID="Image1" runat="server" AlternateText='<%# GetIconText(CType(Eval("section"), boolean), CType(Eval("locked"), boolean)) %>'
                                    ImageURL='<%# GetIcon(CType(Eval("section"), boolean), CType(Eval("locked"), boolean)) %>' />
                            </td>
                            <td width="100%">
                                <b>
                                    <asp:HyperLink Visible='<%# Not CType(Eval("section"), boolean) %>' ID="HyperLink1"
                                        Text='<%# Eval("topic") %>' NavigateUrl='<%# "Threads.aspx?to=" & Eval("topicid").ToString %>'
                                        runat="server" />
                                    <asp:Label Visible='<%# CType(Eval("section"), boolean) %>' ID="Label3" runat="server"
                                        Text='<%# Eval("topic") %>'></asp:Label>
                                </b>
                                <asp:Image SkinID="unread" ID="Image3" runat="server" Visible='<%# IsUnreadTopic(Eval("topicid")) %>' />
                                    
                                <asp:Literal ID="Literal1" Text='<%# iif( CType(Eval("section"), boolean), " - ", "<br />") %>'
                                    runat="server"></asp:Literal>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("description") %>'></asp:Label>
                            </td>
                            <td width="10">
                                <asp:Image skinID="recent" ToolTip='<%# RecentPostAge(Eval("lastpostdate")) %>' ID="Image2" runat="server" Visible='<%# IsRecentPost(Eval("lastpostdate")) %>' />
                            </td>
                            <td align="right">
                                <asp:Label Visible='<%# Not CType(Eval("section"), boolean) %>' ID="Label2" runat="server"
                                    Text='<%# Eval("threadcount") %>'></asp:Label>&nbsp;
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
                <br />
                &nbsp;
                <asp:Image skinID="recent" ImageAlign="Bottom" ID="Image5" runat="server" AlternateText="Recent Message Indicator" />
                &nbsp; One or more messages posted in the last 24 hours.
                <asp:Panel ID="Panel2" runat="server" Width="428px">
                    <br />
                    &nbsp;
                    
                    <asp:Image SkinID="unread" ImageAlign="Bottom" AlternateText="Un-Read Message Indicator"
                        ID="Image3" runat="server" />
                    &nbsp; One or more messages you have not read.<br />
                    <Club:RolloverButton ToolTip="Click to mark all messages read." ID="MarkReadButton" runat="server"
                        OnClick="MarkReadButton_Click" Text="Mark All Read" />
                    
                    <Club:RolloverButton ID="ForumProfileButton" runat="server" OnClick="ForumProfileButton_Click" Text="Forum Profile" />
                </asp:Panel>
            </div>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                SelectCommand="GetWatchedThreads" SelectCommandType="StoredProcedure" DataSourceMode="DataReader">
                <SelectParameters>
                    <asp:Parameter Name="memberid" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
                <HeaderTemplate>
                    <div class="rightblock">
                    <h3>My Watched Threads</h3>
                    <br />
                        <table width="100%">
                            <tr>
                                <td align="left">
                                   </td>
                                <td align="left">
                                    Replies</td>
                                <td align="center">
                                    Last Reply</td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td valign="top" width="100%">
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("threadid", "~/Forum/Thread.aspx?th={0}") %>'
                                Text='<%# Eval("subject") %>' Font-Bold="true"></asp:HyperLink>
                                
                            <asp:Image SkinID="unread"  AlternateText="Un-Read Message Indicator" ToolTip="Un-Read Messages" ID="Image3"
                                runat="server" Visible='<%# IsUnreadThread(Eval("lastpostdate"), Eval("threadid")) %>' />
                        </td>
                        <td align="right" valign="top" width="16">
                            <asp:Label ID="Label4" runat="server" Text='<%# Eval("replies") %>'></asp:Label>
                        </td>
                        <td valign="top">
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("lastpostdate") %>'></asp:Label>
                        </td>
                        <td width="10">
                            <asp:Image SkinID="unread" AlternateText="Recent Message Indicator" ToolTip='<%# RecentPostAge(Eval("lastpostdate")) %>' ID="Image2"
                                 runat="server" Visible='<%# IsRecentPost(Eval("lastpostdate")) %>' />
                        </td>
                        <td width="10" valign="top">
                        
                            <asp:ImageButton SkinID="unwatch" AlternateText="Stop Watching this Thread" 
                                CommandName="Unwatch" CommandArgument='<%# Eval("threadid") %>' ID="Unwatch"
                                runat="server" OnCommand="Unwatch_Command" />
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table> </div>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        <div class="clear2column">
        </div>
    </div>
</asp:Content>
